﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace ErgoBreak.Data
{
    public sealed class Settings : INotifyPropertyChanged
    {
        #region Fields
        private int breaksIntervals;
        private int breakDuration;
        private ObserverType observer;
        private BreakActionType actionType;
        private readonly int BREAK_INTERVALS_DEFAULT = 15;
        private readonly int BREAK_DURATION_DEFAULT = 5;
        private readonly ObserverType DEFAULT_OBSERVER = ObserverType.TimerObserver;
        private readonly BreakActionType DEFAULT_ACTION_TYPE = BreakActionType.TimedAction;
        #endregion

        #region Events
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        #region Constructors
        internal Settings()
        {

        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the time intervals between breaks
        /// </summary>
        public int BreaksIntervals
        {
            get 
            { 
                return this.breaksIntervals; 
            }
            set
            {
                if (value > 0)
                {
                    this.breaksIntervals = value;
                    OnPropertyChanged("BreaksIntervals");
                }
                else
                {
                    throw new ArgumentOutOfRangeException(Messages.GetMessage("Err101"));
                }
            }
        }

        /// <summary>
        /// Gets or sets how long a break takes
        /// </summary>
        public int BreakDuration
        {
            get
            {
                return this.breakDuration;
            }
            set
            {
                if (value > 0)
                {
                    this.breakDuration = value;
                    OnPropertyChanged("BreakDuration");
                }
                else
                {
                    throw new ArgumentOutOfRangeException(Messages.GetMessage("Err101"));
                }
            }
        }

        /// <summary>
        /// Gets or sets the type of observer used
        /// </summary>
        public ObserverType Observer
        {
            get
            {
                return this.observer;
            }
            set
            {
                this.observer = value;
                OnPropertyChanged("Observer");
            }
        }

        /// <summary>
        /// Gets or sets the type of action during a break
        /// </summary>
        public BreakActionType BreakAction
        {
            get
            {
                return this.actionType;
            }
            set
            {
                this.actionType = value;
                OnPropertyChanged("BreakAction");
            }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Resets the settings values to their defaults
        /// </summary>
        public void ResetSettings()
        {
            this.breakDuration = BREAK_DURATION_DEFAULT;
            this.breaksIntervals = BREAK_INTERVALS_DEFAULT;
            this.observer = DEFAULT_OBSERVER;
            this.actionType = DEFAULT_ACTION_TYPE;
        }
        #endregion

        #region Non-Public Methods
        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }
}
